<template>
<div class="enter-page">
    <div class="login-container">
      <div class="login">
        <div class="login-header">
          <h1>Welcome to OkayOJ!</h1>
          <span>请先登录您的账号</span>
        </div>
        <div class="login-form">
          <el-form
              ref="ruleFormRef"
              style="max-width: 680px"
              :model="ruleForm"
              status-icon
              :rules="rules"
              label-width="100px"
              class="demo-ruleForm"
              size="large"
          >
            <el-form-item label="账号或邮箱" prop="pass">
              <el-input v-model="ruleForm.pass" type="" autocomplete="off" 
              placeholder="在此处输入您的账号或邮箱" clearable/>
            </el-form-item>
            <el-form-item label="密码" prop="checkPass">
              <el-input
                v-model="ruleForm.checkPass"
                type="password"
                autocomplete="off"
                placeholder="在此处输入您的密码" clearable
              />
            </el-form-item>
            <el-form-item>
              <el-button type="primary" @click="submitForm(ruleFormRef)">
                登录
              </el-button>
              <el-button @click="resetForm(ruleFormRef)">清空</el-button>
              <el-button @click="newUser">没有账号？立即注册</el-button>
            </el-form-item>
          </el-form>
        </div>
        </div>
      </div>
</div>
</template>
  
<script lang="ts" setup>
/* eslint-disable no-unused-vars */
import { reactive, ref } from 'vue'
import type { FormInstance, FormRules } from 'element-plus'
import router from '../router'
import {useUserStore} from '../store/user'

const userStore=useUserStore();

const ruleFormRef = ref<FormInstance>()

const validatePass = (rule: any, value: any, callback: any) => {
  if (value === '') {
    callback(new Error('请输入您的账号或者邮箱'))
  } else {
    if (ruleForm.checkPass !== '') {
      if (!ruleFormRef.value) return
      ruleFormRef.value.validateField('checkPass')
    }
    callback()
  }
}
const validatePass2 = (rule: any, value: any, callback: any) => {
  if (value === '') {
    callback(new Error('请输入您的密码'))
  } else {
    callback()
  }
}

const ruleForm = reactive({
  pass: '',
  checkPass: '',
  age: '',
  nametag: ''
})

const rules = reactive<FormRules<typeof ruleForm>>({
  pass: [{ validator: validatePass, trigger: 'blur' }],
  checkPass: [{ validator: validatePass2, trigger: 'blur' }],
})

const submitForm = (formEl: FormInstance | undefined) => {
  if (!formEl) return
  formEl.validate((valid) => {
    if (valid) {
      console.log('submit!');

      if(ruleForm.pass.includes('@')){
        ruleForm.nametag='email'
      }else{
        ruleForm.nametag='name'
      }

      submitLogin();
    } else {
      console.log('error submit!')
    }
  })
}

const submitLogin = () => {
   userStore.getUser(ruleForm.pass,ruleForm.nametag,ruleForm.checkPass);
}

const resetForm = (formEl: FormInstance | undefined) => {
  if (!formEl) return
  formEl.resetFields()
}

const newUser = () => {
  console.log('new user')
  router.push({ name: 'RegistPage' })
}
</script>

<style scoped>
.enter-page {
  font-family: Arial, sans-serif;
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100vh;
  background-image: url('../assets/Login-background.jpg');
  background-size: cover;
  .login-container {
    background-color: #f7f8fa;
    border-radius: 8px;
    box-shadow: 0 20px 20px rgba(0, 0, 0, 0.1);
    width: 50vw; 
    min-height: 60vh; 
    align-items: center; 
    .login{
      margin-top:0px;
      .login-header {
        text-align: center;
        margin-top: 5rem;
        
        margin-bottom:2rem;
      }
      .login-form {
        margin-left:30px;
      }
    }
  }
}


</style>
